/*
********************************************************************************
*
*	name : dwt delay
*	file : bsp_dwt.c
********************************************************************************
*/
#define IN_DMA_MOUDLE
#include "bsp.h"
#include "framework.h"
#include "bsp_dma.h"

/**
 * @brief  dma it
 */
dma_it_type dma_it;

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  DMAx stream enable callback
 * @retval None
 */
void dma_nvic_set(const app_dma_type *dmax, uint32_t irq_en, void *callback)
{
	if(dmax->stream > 7)
		return;
	
	if(dmax->DMAx == DMA1)
	{
		nvic_irq_set(dmax->irqn, 5, irq_en);
		(&dma_it.stream10)[dmax->stream] = callback;
	}
	else if(dmax->DMAx == DMA2)
	{
		nvic_irq_set(dmax->irqn, 3, irq_en);
		(&dma_it.stream20)[dmax->stream] = callback;
	}
}

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  DMAx stream cfg
 * @retval None
 */
void dma_stream_config(const app_dma_type *dmax, dma_stream_cfg_type * cfg)
{
	LL_DMA_InitTypeDef dma_cfg;	
	
	dma_cfg.Channel = dmax->trig_src;
	
	// 传输地址和方向
	dma_cfg.PeriphOrM2MSrcAddress = (uu32)cfg->periph;
	dma_cfg.MemoryOrM2MDstAddress = (uu32)cfg->mem;
	
	dma_cfg.Direction = cfg->mem_to_periph? LL_DMA_DIRECTION_MEMORY_TO_PERIPH : LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
	
	// 单次传输
	dma_cfg.Mode = cfg->cycle_mode ? LL_DMA_MODE_CIRCULAR : LL_DMA_MODE_NORMAL;
	
	// 数据格式
	dma_cfg.PeriphOrM2MSrcIncMode = cfg->periph_inc?LL_DMA_PERIPH_INCREMENT : LL_DMA_PERIPH_NOINCREMENT;
	dma_cfg.MemoryOrM2MDstIncMode = cfg->mem_inc? LL_DMA_MEMORY_INCREMENT : LL_DMA_MEMORY_NOINCREMENT;
	dma_cfg.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
	dma_cfg.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
	
	// 数量
	dma_cfg.NbData = cfg->data_len;
	
	// 优先级
	dma_cfg.Priority = cfg->prio;
	
	// fifo
	dma_cfg.FIFOMode = LL_DMA_FIFOMODE_DISABLE;
	dma_cfg.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_4;
	
	// 突发传输
	dma_cfg.MemBurst = LL_DMA_MBURST_SINGLE;
	dma_cfg.PeriphBurst = LL_DMA_PBURST_SINGLE;

	LL_DMA_Init(dmax->DMAx, dmax->stream, &dma_cfg);
}

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream0_IRQHandler(void)
{
	if(dma_it.stream10)
		dma_it.stream10();
}

/**
 * @brief  This function handles DMA1_Stream1_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream1_IRQHandler(void)
{
	if(dma_it.stream11)
		dma_it.stream11();
}

/**
 * @brief  This function handles DMA1_Stream2_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream2_IRQHandler(void)
{
	if(dma_it.stream12)
		dma_it.stream12();
}

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream3_IRQHandler(void)
{
	if(dma_it.stream13)
		dma_it.stream13();
}


/**
 * @brief  This function handles DMA1_Stream4_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream4_IRQHandler(void)
{
	if(dma_it.stream14)
		dma_it.stream14();
}

/**
 * @brief  This function handles DMA1_Stream5_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream5_IRQHandler(void)
{
	if(dma_it.stream15)
		dma_it.stream15();
}

/**
 * @brief  This function handles DMA1_Stream6_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream6_IRQHandler(void)
{
	if(dma_it.stream16)
		dma_it.stream16();
}

/**
 * @brief  This function handles DMA1_Stream7_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_Stream7_IRQHandler(void)
{
	if(dma_it.stream17)
		dma_it.stream17();
}

/**
 * @brief  This function handles DMA2_Stream0_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream0_IRQHandler(void)
{
	if(dma_it.stream20)
		dma_it.stream20();
}

/**
 * @brief  This function handles DMA2_Stream1_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream1_IRQHandler(void)
{
	if(dma_it.stream21)
		dma_it.stream21();
}

/**
 * @brief  This function handles DMA2_Stream2_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream2_IRQHandler(void)
{
	if(dma_it.stream22)
		dma_it.stream22();
}

/**
 * @brief  This function handles DMA2_Stream3_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream3_IRQHandler(void)
{
	if(dma_it.stream23)
		dma_it.stream23();
}

/**
 * @brief  This function handles DMA2_Stream4_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream4_IRQHandler(void)
{
	if(dma_it.stream24)
		dma_it.stream24();
}

/**
 * @brief  This function handles DMA2_Stream5_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream5_IRQHandler(void)
{
	if(dma_it.stream25)
		dma_it.stream25();
}

/**
 * @brief  This function handles DMA2_Stream6_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream6_IRQHandler(void)
{
	if(dma_it.stream26)
		dma_it.stream26();
}

/**
 * @brief  This function handles DMA2_Stream7_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_Stream7_IRQHandler(void)
{
	if(dma_it.stream27)
		dma_it.stream27();
}
